import DerivedSensorReading from "./DerivedSensorReading"

export default class CollectionObserverAdaptor {
  observer: any
  sources: any
  sourceMap: Map<any, any>
  internalReadingMap: Map<any, any>
  readingMap: Map<any, any>
  constructor(t, e) {
    this.observer = t
    this.sources = e
    this.sourceMap = new Map()
    this.internalReadingMap = new Map()
    this.readingMap = new Map()
  }
  onAdded(t, e) {
    const s = this.sources[this.sourceMap.size]
    if (s) {
      this.sourceMap.set(t, s)
      const i = new DerivedSensorReading(e)
      this.internalReadingMap.set(s, i), this.readingMap.set(s, i.facade), this.observer.onAdded && this.observer.onAdded(s, i.facade, this.readingMap)
    }
  }
  onRemoved(t, e) {
    const s = this.sourceMap.get(t)
    if (s) {
      const t = this.readingMap.get(s)
      this.internalReadingMap.delete(s), this.readingMap.delete(s), this.observer.onRemoved && this.observer.onRemoved(s, t, this.readingMap)
    }
  }
  onUpdated(t, e) {
    const s = this.sourceMap.get(t)
    if (s) {
      if ((this.internalReadingMap.get(s).update(e), this.observer.onUpdated)) {
        const t = this.readingMap.get(s)
        this.observer.onUpdated(s, t, this.readingMap)
      }
    }
  }
  onCollectionUpdated() {
    this.observer.onCollectionUpdated && this.observer.onCollectionUpdated(this.readingMap)
  }
}
